package com.example.administrator.hqq.adapter.discover;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.adapter.SetOnClickItem;
import com.example.administrator.hqq.bean.post.PostModuleBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/20 14:27
 * @remark:
 */
public class PostGridAdapter extends BaseAdapter {
    private Context context;
    private List<PostModuleBean.DataBean.PictureUrlBean> list;
    private SetOnClickItem setOnClickItem;
    private int index;
    public void setItemsData(List<PostModuleBean.DataBean.PictureUrlBean> list){

        this.list=list;
    }
    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public PostGridAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.post_grid_item,viewGroup,false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        Glide.with(context).load(list.get(i).getSmall()).fitCenter().into(viewHolder.postImage);
        viewHolder.postImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (setOnClickItem!=null)
                {
                    setOnClickItem.setOnClickItem(i);
                }
            }
        });
        return view;
    }

    public void setSetOnClickItem(SetOnClickItem setOnClickItem) {
        this.setOnClickItem = setOnClickItem;
    }

    static class ViewHolder {
        @BindView(R.id.post_image)
        ImageView postImage;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
